/* IRremoteESP8266: IRsendDemo - demonstrates sending IR codes with IRsend.
 *
 * Version 1.1 January, 2019
 * Based on Ken Shirriff's IrsendDemo Version 0.1 July, 2009,
 * Copyright 2009 Ken Shirriff, http://arcfn.com
 *
 * An IR LED circuit *MUST* be connected to the ESP8266 on a pin
 * as specified by kIrLed below.
 *
 * TL;DR: The IR LED needs to be driven by a transistor for a good result.
 *
 * Suggested circuit:
 *     https://github.com/crankyoldgit/IRremoteESP8266/wiki#ir-sending
 *
 * Common mistakes & tips:
 *   * Don't just connect the IR LED directly to the pin, it won't
 *     have enough current to drive the IR LED effectively.
 *   * Make sure you have the IR LED polarity correct.
 *     See: https://learn.sparkfun.com/tutorials/polarity/diode-and-led-polarity
 *   * Typical digital camera/phones can be used to see if the IR LED is flashed.
 *     Replace the IR LED with a normal LED if you don't have a digital camera
 *     when debugging.
 *   * Avoid using the following pins unless you really know what you are doing:
 *     * Pin 0/D3: Can interfere with the boot/program mode & support circuits.
 *     * Pin 1/TX/TXD0: Any serial transmissions from the ESP8266 will interfere.
 *     * Pin 3/RX/RXD0: Any serial transmissions to the ESP8266 will interfere.
 *   * ESP-01 modules are tricky. We suggest you use a module with more GPIOs
 *     for your first time. e.g. ESP-12 etc.
 */

#include <Arduino.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>

const uint16_t kIrLed = 4;  // ESP8266 GPIO pin to use. Recommended: 4 (D2).  13(D7)

IRsend irsend(kIrLed);  // Set the GPIO to be used to sending the message.

// Example of data captured by IRrecvDumpV2.ino
// 下面这些都是Arduino uno板子接收到的数据，测试后基本上没用                  
// uint16_t rawData[73] = {8880,4520, 580,1620, 580,570, 580,570, 580,570, 630,1620, 580,570, 580,1620, 580,570, 580,570, 630,1570, 630,570, 580,1620, 580,570, 580,570, 580,570, 630,570, 580,570, 580,570, 580,570, 580,570, 630,570, 580,570, 580,570, 580,570, 580,570, 630,570, 580,570, 580,570, 580,1620, 580,570, 630,1620, 580,570, 580,570, 580,1620, 630,570, 580};                         
// uint16_t rawData1[59] = {12380,1020, 130,220, 280,1620, 480,620, 1680,620, 180,2070, 80,1070, 530,1670, 530,670, 480,1720, 480,1720, 480,670, 530,1720, 530,570, 2880,570, 1680,670, 80,1120, 530,620, 3930,620, 530,570, 480,720, 530,970, 1280,670, 530,1720, 480,670, 2730,620, 80,370, 230,520, 2680,720, 580}; // Protocol=UNKNOWN Hash=0xD8996051 30 bits (incl. gap and start) received
//  uint16_t rawData2[73] = {8930,4470, 580,1620, 580,570, 630,570, 580,570, 580,1620, 580,570, 580,1620, 630,570, 580,1620, 580,1620, 580,570, 580,1620, 630,520, 630,570, 580,570, 580,570, 580,570, 630,520, 630,570, 580,570, 580,570, 580,1620, 580,570, 630,570, 580,570, 580,570, 580,570, 580,570, 630,1620, 580,520, 630,1620, 580,570, 630,570, 580,1620, 580,570, 580}; 
//  uint16_t rawData3[73] = {8880,4470, 580,1720, 580,570, 630,570, 630,1670, 580,620, 580,570, 630,1670, 580,620, 580,570, 630,1670, 580,620, 580,1670, 630,570, 580,620, 580,620, 580,570, 630,570, 580,620, 580,620, 580,570, 630,570, 580,1720, 580,620, 580,570, 630,570, 580,620, 580,620, 580,570, 630,1670, 580,620, 580,1670, 580,620, 580,620, 580,1670, 630,570, 580};

// 开启空调，测试有效
 uint16_t rawData6[139] = {9020, 4514, 620, 1610, 620, 550, 620, 550, 620, 1610, 620, 1610, 620, 550, 620, 1612, 618, 552, 620, 1610, 620, 1582, 672, 524, 644, 1586, 620, 550, 620, 522, 648, 524, 670, 526, 644, 526, 620, 526, 646, 548, 622, 550, 620, 550, 646, 1586, 618, 1610, 620, 550, 622, 526, 644, 552, 644, 524, 620, 550, 646, 1586, 644, 526, 620, 1610, 620, 532, 646, 524, 646, 1610, 620, 550, 646, 19994, 642, 1586, 620, 550, 620, 550, 620, 550, 650, 520, 620, 552, 620, 550, 644, 526, 646, 524, 620, 550, 620, 550, 646, 526, 644, 526, 620, 1610, 620, 550, 644, 528, 620, 526, 670, 526, 646, 524, 620, 550, 620, 550, 620, 550, 620, 550, 620, 550, 620, 550, 646, 524, 646, 524, 622, 548, 622, 550, 620, 550, 620, 550, 620, 550, 620}; // GREE
// 关闭空调，测试有效
uint16_t rawData5[139] = {9022, 4512, 620, 1610, 620, 552, 618, 550, 620, 550, 620, 1612, 618, 550, 620, 1612, 620, 524, 646, 1612, 620, 1610, 620, 550, 620, 1610, 620, 550, 620, 550, 620, 552, 620, 552, 620, 552, 620, 552, 618, 550, 620, 550, 620, 550, 620, 1610, 620, 550, 620, 552, 620, 550, 620, 550, 620, 526, 644, 550, 620, 1610, 620, 524, 646, 1610, 620, 560, 620, 550, 620, 1610, 620, 550, 620, 19996, 644, 1610, 620, 550, 620, 550, 620, 550, 620, 552, 620, 550, 620, 550, 620, 552, 620, 550, 620, 550, 620, 550, 620, 550, 620, 550, 620, 1610, 620, 550, 620, 550, 620, 550, 620, 550, 620, 550, 620, 552, 618, 550, 620, 550, 620, 550, 620, 550, 620, 552, 618, 550, 620, 552, 620, 526, 644, 550, 620, 550, 620, 552, 620, 1612, 620}; // GREE20:29:53.867 


// 下面这个是开启空调的，测试有效，信号应该是由小米手机的万能遥控器控制发出来的
uint16_t rawData4[279] = {8990, 4504, 620, 1680, 618, 578, 620, 580, 620, 1680, 620, 580, 620, 580, 620, 1680, 620, 580, 620, 580, 618, 1680, 620, 578, 620, 1680, 620, 554, 644, 578, 620, 580, 620, 580, 620, 580, 620, 580, 620, 580, 618, 582, 618, 554, 644, 1680, 620, 578, 620, 580, 620, 580, 620, 580, 620, 580, 618, 580, 620, 1678, 620, 580, 618, 1680, 618, 580, 618, 580, 618, 1682, 618, 580, 618, 20022, 618, 1680, 618, 580, 620, 580, 620, 580, 618, 1680, 620, 558, 642, 580, 620, 580, 618, 580, 620, 580, 620, 580, 620, 556, 642, 580, 620, 1680, 620, 556, 644, 580, 620, 580, 620, 580, 620, 580, 618, 578, 620, 580, 620, 580, 620, 580, 620, 580, 620, 580, 618, 580, 620, 580, 620, 556, 644, 580, 620, 580, 620, 580, 618, 580, 618, 39996, 9014, 4508, 620, 1680, 620, 580, 620, 580, 620, 1680, 620, 580, 620, 580, 620, 1680, 618, 580, 620, 558, 642, 1680, 618, 580, 618, 1680, 618, 580, 618, 580, 618, 580, 618, 580, 620, 580, 618, 556, 642, 580, 618, 580, 620, 580, 618, 1680, 618, 580, 618, 580, 620, 580, 620, 580, 618, 580, 618, 580, 620, 1680, 620, 1680, 620, 1680, 618, 580, 620, 580, 618, 1658, 642, 580, 620, 20020, 620, 580, 620, 580, 620, 558, 642, 580, 620, 580, 620, 580, 618, 554, 644, 580, 618, 580, 618, 580, 618, 580, 620, 580, 618, 578, 620, 580, 618, 580, 620, 580, 620, 556, 642, 580, 620, 580, 620, 580, 620, 578, 620, 556, 644, 580, 620, 580, 620, 580, 620, 580, 620, 578, 620, 580, 618, 1680, 620, 580, 620, 1680, 618, 1680, 618};


void setup() {
  irsend.begin();
  Serial.begin(115200, SERIAL_8N1, SERIAL_TX_ONLY);
  delay(1000);
}

void loop() {

  delay(5000);
  Serial.println("first turn on");
  irsend.sendRaw(rawData6, 139, 38);  // Send a raw data capture at 38kHz.
  delay(15000);
  Serial.println("first turn off ac");
  irsend.sendRaw(rawData5, 139, 38);  // Send a raw data capture at 38kHz.
  delay(20000);
  // 开启空调有效
  Serial.println("valid turn on");
  irsend.sendRaw(rawData4, 279, 38);  // Send a raw data capture at 38kHz.
  delay(15000);
  // 关闭空调
  Serial.println("first turn off ac");
  irsend.sendRaw(rawData5, 139, 38);  // Send a raw data capture at 38kHz.
  delay(2000);

  // Serial.println("turn on");
  // irsend.sendNEC(0x490A205011200000,64);
  // delay(2000);
  // irsend.sendNEC(0x590B605001200000,64);
  // delay(2000);
  
  // Serial.println("turn off");
  // irsend.sendNEC(0x410A205011200080,64);
  // delay(2000);
  // // 关机
  // irsend.sendNEC(0x510B205001200080,64);
  // delay(2000);
}
